package com.pkutech.carcasher.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.dd.processbutton.iml.ActionProcessButton;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.bean.BaseResponse;
import com.pkutech.carcasher.network.SoapApi;
import com.pkutech.carcasher.utils.ToastUtil;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * 提交车位预定
 * Created by cyy on 2016/6/10.
 */
public class StopParkingActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.carNumEditText)
    MaterialEditText carNumEditText;
    @BindView(R.id.carPositionEditText)
    MaterialEditText carPositionEditText;

    @BindView(R.id.stopAddBtn)
    ActionProcessButton stopAddBtn;
    @BindView(R.id.spinner1)
    Spinner spinner1;
    @BindView(R.id.spinner2)
    Spinner spinner2;
    @BindView(R.id.spinner3)
    Spinner spinner3;

    List<String> provinceList;
    List<String> letterList;
    List<String> carTypeList;

    String curProvince;
    String curLetter;
    String carType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_stop);
        ButterKnife.bind(this);
        initToolbar(toolbar);
//        carNumEditText.setText("苏B");

        String[] provinces = getResources().getStringArray(R.array.provinces);
        provinceList = Arrays.asList(provinces);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.park_name_spinner_item,
                R.id.text1, provinceList);
        spinner1.setAdapter(adapter1);
        spinner1.setSelection(0);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                curProvince = provinceList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        String[] letters = getResources().getStringArray(R.array.letters);
        letterList = Arrays.asList(letters);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.park_name_spinner_item,
                R.id.text1, letterList);
        spinner2.setAdapter(adapter2);
        spinner2.setSelection(0);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                curLetter = letterList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        String[] carTypes = getResources().getStringArray(R.array.car_type);
        carTypeList = Arrays.asList(carTypes);
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, R.layout.park_name_spinner_item,
                R.id.text1, carTypeList);
        spinner3.setAdapter(adapter3);
        spinner3.setSelection(0);
        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String carTypeStr = carTypeList.get(position);
                if("小型车".equals(carTypeStr)){
                    carType = "1";
                }else if("中型车".equals(carTypeStr)){
                    carType = "2";
                }else if("大型车".equals(carTypeStr)){
                    carType = "3";
                }else{
                    carType = "0";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @OnClick(R.id.stopAddBtn)
    public void stopAdd() {

        if (TextUtils.isEmpty(carNumEditText.getText().toString())) {
            ToastUtil.showInfoToast(this, "请输入车牌号", ToastUtil.Position.TOP);
            return;
        }
        if("0".equals(carType)){
            ToastUtil.showInfoToast(this, "请选择车型", ToastUtil.Position.TOP);
            return;
        }
        if (TextUtils.isEmpty(carPositionEditText.getText().toString())) {
            ToastUtil.showInfoToast(this, "请输入车位号", ToastUtil.Position.TOP);
            return;
        }

        String fullCarNum = "";
        if (!TextUtils.isEmpty(curProvince)) {
            fullCarNum += curProvince;
        }
        if (!TextUtils.isEmpty(curLetter)) {
            fullCarNum += curLetter;
        }
        if (!TextUtils.isEmpty(carNumEditText.getText())) {
            fullCarNum += carNumEditText.getText().toString();
        }
        if (!fullCarNum.matches("^[\\u4e00-\\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{5}$")) {
            ToastUtil.showErrorToast(StopParkingActivity.this, "请输入正确的车牌号", ToastUtil.Position.TOP);
            return;
        }

        SoapApi.stopAdd(fullCarNum, carPositionEditText.getText().toString(), carType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        stopAddBtn.setProgress(1);
                    }
                })
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {
                        stopAddBtn.setProgress(0);
                    }

                    @Override
                    public void onError(Throwable e) {
                        stopAddBtn.setProgress(0);
                        ToastUtil.showInfoToast(StopParkingActivity.this, "停靠失败", ToastUtil.Position.TOP);
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        stopAddBtn.setProgress(0);
                        if (baseResponse != null && !TextUtils.isEmpty(baseResponse.getStatus()) && "true".equals(baseResponse.getStatus())) {
                            ToastUtil.showSuccessToast(StopParkingActivity.this, "停靠成功", ToastUtil.Position.TOP);
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else {
                            if (baseResponse != null && !TextUtils.isEmpty(baseResponse.getMessage())) {
                                ToastUtil.showInfoToast(StopParkingActivity.this, baseResponse.getMessage(), ToastUtil.Position.TOP);
                            } else {
                                ToastUtil.showInfoToast(StopParkingActivity.this, "停靠失败", ToastUtil.Position.TOP);
                            }
                        }
                    }
                });

    }
}
